﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;

using WlanDesign;
using Autodesk.AutoCAD.Interop;

namespace WlanDesignDll
{
    public partial class FormAttenuationSetSub1 : Form
    {
        //private static Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
        //private static Database acCurDb = acDoc.Database;
        // ini文件与当前文档在同一目录下，并且名字相同
        //private static string[] name = acDoc.Name.Split('.');
        //private static string iniName = name[0] + ".ini";

        //private  Entity acEnt = null;
        public FormAttenuationSetSub1()
        {
            //this.acEnt = ent;
            InitializeComponent();
        }

        private void FormAttenuationSetSub1_Load(object sender, EventArgs e)
        {
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            // ini文件与当前文档在同一目录下，并且名字相同
            string[] name = acDoc.Name.Split('.');
            string iniName = name[0] + ".ini";
            string entId1 = ReadIniTxt(iniName, "handle", "handlevalue");
            long enth = long.Parse(entId1);

            Handle entId = new Handle(enth);
            // 设置对象属性

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 以只读方式打开块表记录   Open the Block table record for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                // 以写方式打开模型空间块表记录   Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;

                Entity acEnt = acTrans.GetObject(acCurDb.GetObjectId(false, entId, 0), OpenMode.ForRead) as Entity;
                if (acEnt == null)
                {
                    this.Close();
                }
                var rb = acEnt.GetXDataForApplication("degreeofdecay");
                if (rb == null)
                {
                    this.tBox_Penet.Text = "";
                }
                else
                {
                    List<TypedValue> values = rb.Cast<TypedValue>().ToList();
                    string value1 = Convert.ToString(values[1].Value);
                    this.tBox_Penet.Text = value1;
                }
            } 
        }

        private void btn_Do_Click(object sender, EventArgs e)
        {
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            // ini文件与当前文档在同一目录下，并且名字相同
            string[] name = acDoc.Name.Split('.');
            string iniName = name[0] + ".ini";
            if (this.tBox_Penet.Text == "")
            {
                MessageBox.Show("请输入透射衰减度！");
                this.tBox_Penet.Focus();
                return;
            }
            string penet = this.tBox_Penet.Text;
            if (isNumberic(penet))
            {
                string[] name1 = acDoc.Name.Split('.');
                string iniName1 = name1[0] + ".ini";
                // 写入ini文件
                WriteIniTxt(iniName1, "attenuationsub", "penetrance_attenuationsub", penet);

                //acDoc.SendStringToExecute("AttenuationSetSub1", true, false, false);
                Autodesk.AutoCAD.Interop.AcadApplication acAppComObj = null;
                const string strProgId = "AutoCAD.Application.18";
                // Get a running instance of AutoCAD
                try
                {
                    acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
                }
                catch // An error occurs if no instance is running
                {
                    try
                    {
                        // Create a new instance of AutoCAD
                        acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);

                    }
                    catch
                    {
                        // If an instance of AutoCAD is not created then message and exit
                        System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" + " could not be created.");
                        return;
                    }
                }
                AcadDocument acDocComObj;

                acDocComObj = acAppComObj.ActiveDocument;
                acDocComObj.SendCommand("AttenuationSetSub1 ");

                MessageBox.Show("设定成功！");
                this.Close();
            }
            else
            {
                MessageBox.Show("衰减度格式不正确！");
            }
        }
        //
        // Summery
        // 加载信息到ini文件
        //
        public void WriteIniTxt(string sPath, string Section, string Key, string value)
        {
            string ServerName = value;

            IniFileRW ini = new IniFileRW(sPath);

            ini.IniWriteValue(Section, Key, ServerName);
        }
        //
        // Summery
        // 从ini文件读取信息
        //
        public static string ReadIniTxt(string sPath, string Section, string Key)
        {
            string ServerName;
            IniFileRW ini = new IniFileRW(sPath);
            ServerName = ini.IniReadValue(Section, Key);
            string strSql = ServerName;
            return strSql;
        }
        /// <summary>
        /// 判断字符串是否为数字
        /// </summary>
        public static bool isNumberic(string message)
        {
            System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(@"^(-?\d+)(\.\d+)?$");
            if (rex.IsMatch(message))
            {
                return true;
            }
            else
                return false;
        }

        private void btn_Undo_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
